package jm.mq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import jm.mq.utils.RabbitMQUtils;

import java.nio.charset.StandardCharsets;

/**
 * @Description 手动应答消费者1
 * @date 2022/3/5 19:17
 */
public class Worker1 {

    private static final String TASK_QUEUE_RES = "queue_res";

    public static void main(String[] args)  throws Exception{
        Channel channel = RabbitMQUtils.getChannel();
        System.out.println("线程A等待接收......");

        DeliverCallback deliverCallback = (consumerTag, message) -> {
            // 模拟并发沉睡一秒
            try {
                Thread.sleep(1000);
                System.out.println("线程A接收消息："+ new String(message.getBody(), StandardCharsets.UTF_8));
                /**
                 * basicAck:
                 *          1. 消息标记
                 *          2. 是否批量
                 */
                channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };
        channel.basicConsume(TASK_QUEUE_RES,false,deliverCallback,
                consumerTag -> {
                    System.out.println(consumerTag + "消费者取消消费");
                });

    }
}

